package com.ddapi.ddapiinterface.controller;


import com.dd.ddapiclientsdk.model.User;
import com.dd.ddapiclientsdk.utils.SignUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//查询作者名称api
@RestController
@RequestMapping("name")
public class NameController {
    @GetMapping("/get")
    public String getNameByGet(String name){

        return "GET 你的名字是："+name;
    }

    @PostMapping("/post")
    public String getNameByPost(@RequestParam String name){
        return "POST 你的名字是:"+name;
    }

    @PostMapping("/user")
    public String getUsernameByPost(@RequestBody User user, HttpServletRequest request){
//        String accessKey=request.getHeader("accessKey");
//        String nonce=request.getHeader("nonce");
//        String timeStamp=request.getHeader("timeStamp");
//        String sign=request.getHeader("sign");
//        String body=request.getHeader("body");
//        //TODO 实际情况应该是去数据库中查是否已分配给用户
//        if(!accessKey.equals("ddcr")){
//            throw new RuntimeException("无权限！");
//        }
//        if(Long.parseLong(nonce)>10000){
//            throw new RuntimeException("nonce有误！");
//        }
//        //TODO 事件和当前事件不能超过5分钟
//        String currentTimeStamp=String.valueOf(System.currentTimeMillis()/1000);
//        if(Math.abs(Long.parseLong(currentTimeStamp)-Long.parseLong(timeStamp))/60>5){
//            throw new RuntimeException("时间戳超时！");
//        }
//        //TODO 实际情况是从数据库中查出secretKey
//        String serverSign= SignUtils.genSign(body,"abcdef");
//        if(!sign.equals(serverSign)){
//            throw new RuntimeException("安全性验证失败！");
//        }
        String result="POST 用户名字是:"+user.getUsername();
        //调用次数+1
        return result;
    }
}
